class TreeNode:
    def __init__(self,data):
        self.data=data
        self.right=None
        self.left=None

def preorder(node):
    if node:
        print(node.data,end=' ')
        preorder(node.left)
        preorder(node.right)
def inorder(node):
    if node:
        inorder(node.left)
        print(node.data, end=' ')
        inorder(node.right)
def postorder(node):
    if node:
        postorder(node.left)
        postorder(node.right)
        print(node.data, end=' ')
def count_leaves(node):
    if node == None:
        return 0
    elif (node.left==None) and (node.right==None):
        return 1
    else:
        left_leaves=count_leaves(node.left)
        right_leaves=count_leaves(node.right)
        return left_leaves+right_leaves
#批量输入
def build_binary_tree():
   data = input("请输入一组数据，以空格分隔：").split()
   root = TreeNode(data[0])
   stack = [root]
   i = 1
   while i < len(data):
       current_node = stack.pop()
# 左子树
       if data[i] != '#':
           current_node.left = TreeNode(data[i])
           stack.append(current_node.left)
       i += 1
       # 右子树
       if i < len(data) and data[i] != '#':
           current_node.right = TreeNode(data[i])
           stack.append(current_node.right)
       i += 1
   return root
# # 主程序2
# if __name__ == "__main__":
#    root = build_binary_tree()
#    print("先序遍历结果：", end=" ")
#    preorder(root)
#    print()
#    print("中序遍历结果：", end=" ")
#    inorder(root)
#    print()
#    print("后序遍历结果：", end=" ")
#    postorder(root)
#    print()
# 主程序1
if __name__ == "__main__":
    T1 = TreeNode(1)
    T2 = TreeNode(2)
    T3 = TreeNode(3)
    T4 = TreeNode(4)
    T5 = TreeNode(5)
    T1.left = T2
    T1.right = T3
    T2.left = T4
    T2.right = T5
    root = T1
    print("先序遍历结果：", end=" ")
    preorder(root)
    print()
    print("中序遍历结果：", end=" ")
    inorder(root)
    print()
    print("后序遍历结果：", end=" ")
    postorder(root)
    print()
    print(count_leaves(root))



